package com.example.cloud.controller.business.reservation;

import com.alibaba.fastjson.JSON;
import com.example.cloud.constants.CommonConstants;
import com.example.cloud.controller.common.BaseController;
import com.example.cloud.model.business.reservation.Reservation;
import com.example.cloud.model.dto.QueryModel;
import com.example.cloud.model.dto.Result;
import com.example.cloud.model.system.SystemConfig;
import com.example.cloud.service.business.reservation.ReservationService;
import com.example.cloud.service.system.SystemConfigService;
import com.example.cloud.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * Created by wangtao on 2018/1/26.
 */
@RestController
@RequestMapping("/reservation")
public class ReservationController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(ReservationController.class);

    @Autowired
    private ReservationService reservationService;

    @Autowired
    private SystemConfigService systemConfigService;

    @PostMapping("/search")
    public Result search(@RequestBody QueryModel model) {
        return this.singleQuery(model, Reservation.class);
    }

    /**
     * 预约确认
     * @param records
     * @return
     */
    @PostMapping("/saveAppointment")
    public void saveAppointment(@RequestBody List<Reservation> records) throws ParseException {
        reservationService.doSave(records);
    }

    /**
     * 改约校验
     * @param id
     * @return
     */
    @GetMapping("/checkChange/{id}")
    public Result checkChange(@PathVariable Long id){
        return reservationService.doCheckChange(id);
    }

    /**
     * 改约
     * @param record
     * @return
     */
    @PostMapping("/change")
    public Result change(@RequestBody @Valid Reservation record, BindingResult results) throws Exception {
        if (results.hasErrors()) {
            String message = results.getFieldError().getDefaultMessage();
            return ResultUtil.fail(message);
        }
        reservationService.doChange(record);
        return ResultUtil.success();
    }

    @PostMapping("/checkDelete")
    public Result checkDelete(@RequestBody List<Long> ids){
        return ResultUtil.success(reservationService.doCheckDelete(ids));
    }

    @PostMapping("/delete")
    public void delete(@RequestBody List<Long> ids){
        List<Long> canList = reservationService.doCheckDelete(ids);
        if(!canList.isEmpty()){
            reservationService.doDelete(ids);
        }
    }

    /**
     * 今日接待
     * @return
     */
    @PostMapping("/reception")
    public Result reception(@RequestBody Map params){
        return reservationService.searchReservation(params);
    }

    /**
     * 电话随访
     */
    @PostMapping("/visit")
    public Result visit(@RequestBody Map params){
        return reservationService.searchVisit(params);
    }

    /**
     * 今日完成
     */
    @PostMapping("/complete")
    public Result complete(@RequestBody Map params){
        int process = Integer.parseInt(params.get("process").toString());
        QueryModel q = JSON.parseObject(JSON.toJSONString(params.get("queryModel")), QueryModel.class);
        return reservationService.searchComplete(process, q);
    }

    /**
     * 历史未完成
     */
    @PostMapping("/undone")
    public Result undone(@RequestBody Map params){
        int process = Integer.parseInt(params.get("process").toString());
        QueryModel q = JSON.parseObject(JSON.toJSONString(params.get("queryModel")), QueryModel.class);
        return reservationService.searchUndone(process, q);
         
    }

    /**
     * 拒绝治疗
     */
    @PostMapping("/refuseTreat")
    public Result refuseTreat(@RequestBody Map params){
        QueryModel q = JSON.parseObject(JSON.toJSONString(params.get("queryModel")), QueryModel.class);
        return reservationService.searchRefuseTreat(q);
    }

    /**
     * 支持
     * @param record
     */
    @PostMapping("/support")
    public void support(@RequestBody Reservation record){
        reservationService.doSupport(record);
    }

    /**
     * 回访确认
     */
    @PostMapping("/visitConfirm/{id}")
    public void visitConfirm(@PathVariable Long id){
        reservationService.doVisitConfirm(id);
    }

    /**
     * 重新预约
     * @param record
     */
    @PostMapping("/appointment")
    public void appointment(@RequestBody Reservation record){
        reservationService.doAppointment(record);
    }

    /**
     * 取消校验
     * @param id
     * @return
     */
    @GetMapping("/checkCancel/{id}")
    public Result checkCancel(@PathVariable Long id){
        return ResultUtil.success(reservationService.doCheckCancel(id));
    }

    /**
     * 取消
     * @param id
     * @return
     */
    @PostMapping("/cancel/{id}")
    public void cancel(@PathVariable Long id){
        reservationService.doCancel(id);
    }

    /**
     * 到场校验
     * @param id
     * @return
     */
    @GetMapping("/checkArrived/{id}")
    public Result checkArrived(@PathVariable Long id){
        return ResultUtil.success(reservationService.doCheckArrived(id));
    }

    /**
     * 到场
     * @param id
     * @return
     */
    @PostMapping("/arrived/{id}")
    public void arrived(@PathVariable Long id){
        reservationService.doArrived(id);
    }

    @GetMapping("/checkAdmission/{id}")
    public Result checkAdmission(@PathVariable Long id){
        return ResultUtil.success(reservationService.doCheckAdmission(id));
    }

    @PostMapping("/admission/{id}")
    public void admission(@PathVariable Long id){
        reservationService.doAdmission(id);
    }

    @PostMapping("/treatComplete/{id}")
    public void treatComplete(@PathVariable Long id){
        reservationService.doTreatComplete(id);
    }

    @PostMapping("/visitComplete/{id}")
    public void visitComplete(@PathVariable Long id){
        reservationService.doVisitComplete(id);
    }

    /**
     * 完成校验
     * @param id
     * @return
     */
    @GetMapping("/checkComplete/{id}")
    public Result checkComplete(@PathVariable Long id){
        return ResultUtil.success(reservationService.doCheckComplete(id));
    }

    /**
     * 预约确认
     */
    @PostMapping("/confirm/{id}")
    public void confirm(@PathVariable Long id){
        reservationService.doConfirm(id);
    }

    /**
     * 完成
     * @param id
     * @return
     */
    @PostMapping("/complete/{id}")
    public void complete(@PathVariable Long id){
        reservationService.doComplete(id);
    }

    @PostMapping("/editNote/{id}/{note}")
    public void editNote(@PathVariable Long id, @PathVariable String note){
        reservationService.doEditNote(id, note);
    }

    /**
     * 预约校验
     * @param record
     * @return
     * @throws ParseException
     */
    @PostMapping("/checkAppointment")
    public Result checkAppointment(@RequestBody Reservation record) throws ParseException {
        Result result = new Result();
        SystemConfig config = systemConfigService.findOne();
        if(config == null){
            result.setCode(CommonConstants.FAILURE);
            result.setMsg("系统参数未配置");
        }
        return reservationService.doCheckAppointment(record, config);
    }

    @GetMapping("/preview")
    public Result preview(){
        try {
            reservationService.sendPreview();
        }catch (Exception e){
            return ResultUtil.fail();
        }
        return ResultUtil.success();
    }
}
